package com.tedu.dao.impl;

import com.tedu.dao.UserDao;
import com.tedu.entity.User;
import com.tedu.utils.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    @Override
    public User loginUser(User user) throws SQLException {

        Connection conn = DataSource.getConnection();
        PreparedStatement preparedStatement =
                conn.prepareStatement("select * from user where username = ? and password= ?");
        preparedStatement.setString(1, user.getUsername());
        preparedStatement.setString(2, user.getPassword());
        ResultSet resultSet = preparedStatement.executeQuery();
        User user1 = null;
        if (resultSet.next()){
            user1 = new User().setId(resultSet.getInt(1))
                    .setUsername(resultSet.getString(2))
                    .setRealname(resultSet.getString(3))
                    .setPassword(resultSet.getString(4))
                    .setSex(resultSet.getString(5));
        }
        return user1;
    }
}
